package com.ruoyi.applet.controller;

import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.applet.domain.*;
import com.ruoyi.applet.domain.vo.OrderByUserId;
import com.ruoyi.applet.domain.vo.UserAddress;
import com.ruoyi.applet.service.*;
import com.ruoyi.applet.utli.TokenByUsrId;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单列表Controller
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/applet")
public class OrderController extends BaseController
{
    @Resource
    private IOrderService orderService;

    @RequestMapping("/orderList")
    public Object selectOrderByUsrId(@RequestParam("token") String token){
        List<Order> orderList = orderService.selectOrderUerIdOrderList(TokenByUsrId.tokenByUsrId(token));
        List<OrderByUserId> orderByUserIds = new ArrayList<>();
        for (Order order : orderList) {
            orderByUserIds.add(orderService.selectOrderUerIdList(TokenByUsrId.tokenByUsrId(token),order.getOrderGoodsId()));
        }
        return orderByUserIds;
    }

    @RequestMapping("/orderStatus")
    public Object selectOrderStatus(@RequestParam("token") String token){
        Map<String,Object> objectMap = new HashMap<>();
        List<OrderStatus> statusList = orderService.selectAllByOrderIdStatusList();
        List<Integer> statusResult = new ArrayList<>();
        for (OrderStatus orderStatus : statusList) {
            System.out.println(orderStatus.getOrderStatusId());
            OrderByUserId order = orderService.selectByOrderStatusInteger(TokenByUsrId.tokenByUsrId(token),orderStatus.getOrderStatusId());
            statusResult.add(order.getStatusCount());
        }
        objectMap.put("statusList",statusList);
        objectMap.put("statusResult",statusResult);
        return objectMap;
    }

    @RequestMapping("/orderType")
    public List<OrderByUserId> selectOrderType(@RequestParam("token") String token, @RequestParam("orderStatus") Long orderStatus){
        List<OrderByUserId> orderByUserIds = orderService.selectOrderProductSpecValueIdList(TokenByUsrId.tokenByUsrId(token),orderStatus);
        for (OrderByUserId orderByUserId : orderByUserIds) {
            System.out.println(orderByUserId.getOrderId());
        }
        return orderByUserIds;
    }

    @RequestMapping("/orderStatusList")
    public Object selectOrderStatusLst(@RequestParam("token") String token){
        return orderService.selectByOrderStatusList(TokenByUsrId.tokenByUsrId(token));
    }

    @Resource
    ISpikesInfoService spikesInfoService;

    @RequestMapping("/orderByUsrIdAndOrderId")
    public Boolean deleteOrderByUsrId(@RequestParam("token") String token,@RequestParam("orderId") String orderId){
        Order order = orderService.selectOrderNotesOrder(orderId);
        if(order.getOrderNotes() == null){
            return orderService.updateByOrderIdInteger(TokenByUsrId.tokenByUsrId(token),orderId) == 1;
        }else{
            orderService.updateByOrderIdInteger(TokenByUsrId.tokenByUsrId(token),orderId);
            SpikesInfo spikesInfo = spikesInfoService.selectAllBySpikesGoodIdSpikesInfo(order.getOrderGoodsId(), order.getOrderSpikesInfo());
            return spikesInfoService.updateSpikesNumber(order.getOrderGoodsId(), spikesInfo.getSpikesNumber() + 1, spikesInfo.getSpikesSold() - 1, order.getOrderSpikesInfo()) == 1;
        }
    }

    @RequestMapping("/orderByUsrIdAndOrderIdAndStatus")
    public Boolean updateOrderByStatus(@RequestParam("token") String token,@RequestParam("status") Long status,@RequestParam("orderId") String orderId){
        return orderService.updateOrderIdAndOrderUerIdInteger(TokenByUsrId.tokenByUsrId(token),status+1,orderId) == 1;
    }

    @Resource
    IAddressService addressService;

    @RequestMapping("/orderByUsrIdAndAddress")
    public Object selectOrderByUsrIdAddress(@RequestParam("token") String token,@RequestParam("orderId") String orderId){
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("userAddress",addressService.selectAllByAddressUsrId(TokenByUsrId.tokenByUsrId(token)));
        objectMap.put("orderGoods",orderService.selectAllByOrderIdAndOrderUerIdOrderByUserId(TokenByUsrId.tokenByUsrId(token),orderId));
        return objectMap;
    }

    @RequestMapping("/orderInsert")
    public Object insertOrderByUserId(@RequestParam("token") String token,@RequestParam("orderId") String orderId,@RequestParam("orderTotal") String orderTotal,
                                      @RequestParam("orderGoodsId") Long orderGoodsId, @RequestParam("orderNumber") Long orderNumber){
        UserAddress address = addressService.selectAllByAddressUsrId(TokenByUsrId.tokenByUsrId(token));
//        Order order = orderService.selectAllByOrderGoodsIdCount(TokenByUsrId.tokenByUsrId(token),orderGoodsId);
//        if(order != null){
//            return orderService.updateOrderGoodsIdAndOrderUerIdInteger(TokenByUsrId.tokenByUsrId(token),order.getOrderId(),orderNumber+order.getOrderNumber());
//        }else{
        System.out.println(orderTotal);
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 18; i++) {
            sb.append(random.nextInt(10)); // 生成0到9之间的随机数字
        }
        return orderService.insertAllInteger(TokenByUsrId.tokenByUsrId(token),orderId, orderTotal, address.getUsrName(), address.getAddressPhone(), address.getAddressDesc() + address.getAddressText(),orderGoodsId,orderNumber,sb.toString());
//        }
    }



    @RequestMapping("/orderByUsrId")
    public Object deleteOrderByUserId(@RequestParam("token") String token,@RequestParam("orderId") String orderId){
        return orderService.deleteByOrderIdInteger(TokenByUsrId.tokenByUsrId(token),orderId);
    }

    @Resource
    IProductSpecsService productSpecsService;

    @RequestMapping("/updateOrderStatus")
    public Object updateOrderStatusAndUsrId(@RequestParam("orderId") String orderId, @RequestParam("token") String token,
                                            @RequestParam("price")BigDecimal price){
        if(orderService.updateOrderIdAndOrderUerId(orderId,TokenByUsrId.tokenByUsrId(token),price) == 1){
            Order order = orderService.selectOrderUerIdOrder(orderId);
            ProductSpecs productSpecs = productSpecsService.selectAllByProductSpecIdProductSpecs(order.getOrderGoodsId());
            if(productSpecs.getProductStock() > order.getOrderNumber()){
                if(productSpecsService.updateProductNumberAndProductSpecId(productSpecs.getProductStock()-order.getOrderNumber(),productSpecs.getProductSpecValueId()) == 1){
                    return true;
                }
            }
        }
        return false;
    }
}
